﻿using System;
using System.Collections.Generic;
using System.Linq;
using Logger = BepInEx.Logger;
using BepInEx.Logging;

namespace CheatMenu.Utils
{
    public class CommandGroup
    {
        public string Name { get; set; }
        public List<Command> Commands { get; private set; } = new List<Command>();

        public CommandGroup(string name)
        {
            Name = name;
        }

        public void ClearCommands()
        {
            Commands.Clear();
        }

        public void AddCommand(string name, Action<Command> action, bool toggleState = false)
        {
            if (Commands.Where((c) => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase)).Count() == 0)
                Commands.Add(new Command(name, action, toggleState));
            else
                Logger.Log(LogLevel.Warning, $"Trying to add existing command with name: {name}");
        }

        public void RemoveCommand(string name)
        {
            var existingCommand = Commands.Where((c) => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
            if (existingCommand.Count() == 1)
                Commands.Remove(existingCommand.First());
            else
                Logger.Log(LogLevel.Warning, $"Trying to remove non-existing command with name: {name}");
        }
    }
}
